package com.dong.oss.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import io.minio.MinioClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 客户端配置类
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties({MinioProperties.class, OssProperties.class, MigrationProperties.class})
public class ClientConfiguration {
    
    /**
     * 创建MinIO客户端Bean
     */
    @Bean
    @ConditionalOnProperty(prefix = "minio", name = {"endpoint", "access-key", "secret-key", "bucket-name"})
    public MinioClient minioClient(MinioProperties minioProperties) {
        log.info("创建MinIO客户端，端点: {}, 存储桶: {}", minioProperties.getEndpoint(), minioProperties.getBucketName());
        return MinioClient.builder()
                .endpoint(minioProperties.getEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
    }
    
    /**
     * 创建阿里云OSS客户端Bean
     */
    @Bean
    @ConditionalOnProperty(prefix = "oss", name = {"endpoint", "access-key-id", "access-key-secret", "bucket-name"})
    public OSS ossClient(OssProperties ossProperties) {
        log.info("创建OSS客户端，端点: {}, 存储桶: {}", ossProperties.getEndpoint(), ossProperties.getBucketName());
        log.debug("OSS配置详情 - endpoint: {}, accessKeyId: {}, bucketName: {}", 
                ossProperties.getEndpoint(),
                ossProperties.getAccessKeyId(),
                ossProperties.getBucketName());
        
        if (ossProperties.getEndpoint() == null) {
            log.error("OSS endpoint不能为null！当前配置: {}", ossProperties);
        }
        
        return new OSSClientBuilder().build(
                ossProperties.getEndpoint(),
                ossProperties.getAccessKeyId(),
                ossProperties.getAccessKeySecret()
        );
    }
}